/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package phd.changeidentifiers.testing.display.frames;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
 *
 * @author Max
 */
public abstract class GraphPanel extends JPanel{
    private int minX;
    private int minY;
    private int maxX;
    private int maxY;
    private int scaleY = 1;
    private int scaleX = 1;
    private String xAxisTitle;
    private String yAxisTitle;

    public Image getImage(){
        Dimension screenDim = getScreenDimensions();
        Image image = createImage(screenDim.width, screenDim.height);
        paint(image.getGraphics());
        return image;
    }
    public void writeImage(File file, String extension) throws IOException{
        BufferedImage image = (BufferedImage)getImage();
        ImageIO.write(image, extension, file);
    }
    public void setXRange(int min, int max){
        this.minX = min;
        this.maxX = max;
    }

    public void setYRange(int min, int max){
        this.minY = min;
        this.maxY = max;
    }
    public Point getOrigin(){
        return new Point(20, 20);
    }
    public Point getScreenPosition(int x, int y){
        return getScreenPosition(new Point(x, y));
    }
    public Point getScreenPosition(Point point){
        Dimension screenDim = getScreenDimensions();
        return new Point(getOrigin().x+(point.x*getScaleX()), screenDim.height-(getOrigin().y+(point.y*getScaleY())));
    }
    public Dimension getScreenDimensions(){
        int height = (this.getMaxY()*this.getScaleY())+50;//this.getHeight()
        int width = (this.getMaxX()*this.getScaleX())+50;
        return new Dimension(width, height);
    }
    @Override
    public void paint(Graphics g){
//        super.paint(g);
        Graphics2D g2 =  (Graphics2D) g;
        g2.setColor(Color.WHITE);
        Dimension screenDim = getScreenDimensions();
        g2.fillRect(0, 0, screenDim.width, screenDim.height);
        g2.setColor(Color.BLACK);
        Point origin = getScreenPosition(new Point(0, 0));
        Point maxTimeSteps = getScreenPosition(new Point(maxX, 0));
        Point maxDifference = getScreenPosition(new Point(0, maxY));
        g2.drawLine(origin.x, origin.y, maxTimeSteps.x, maxTimeSteps.y);
        g2.drawLine(origin.x, origin.y, maxDifference.x, maxDifference.y);

        Point origin_text = getScreenPosition(new Point(0, 0));
        g2.drawString(""+getMinX(), origin_text.x-3, origin_text.y+13);
        g2.drawString(""+getMinY(), origin_text.x-13, origin_text.y+3);

        Point maxX_text = getScreenPosition(new Point(maxX, 0));
        g2.drawString(""+getMaxX(), maxX_text.x, maxX_text.y+3);

        Point maxY_text = getScreenPosition(new Point(0, (int)Math.ceil(maxY)));
        g2.drawString(""+getMaxY(), maxY_text.x-3, maxY_text.y-5);

        Point x_axis_text = getScreenPosition(new Point(maxX/2, 0));
        g2.drawString(getxAxisTitle(), x_axis_text.x-20, x_axis_text.y+13);

        Point y_axis_text = getScreenPosition(new Point(0, ((int)Math.ceil(maxY)/2)));
//        AffineTransform previous = g2.getTransform();
//        AffineTransform current = previous;
//        current.rotate(Math.toRadians(90));
        g2.rotate(Math.PI/2);
        g2.drawString(getyAxisTitle(), y_axis_text.y-23, y_axis_text.x-23);
        g2.rotate(-Math.PI/2);
    }

    /**
     * @return the minX
     */
    public int getMinX() {
        return minX;
    }

    /**
     * @return the minY
     */
    public int getMinY() {
        return minY;
    }

    /**
     * @return the maxX
     */
    public int getMaxX() {
        return maxX;
    }

    /**
     * @return the maxY
     */
    public int getMaxY() {
        return maxY;
    }

    /**
     * @return the scaleY
     */
    public int getScaleY() {
        return scaleY;
    }

    /**
     * @return the scaleX
     */
    public int getScaleX() {
        return scaleX;
    }

    /**
     * @param scaleY the scaleY to set
     */
    public void setScaleY(int scaleY) {
        this.scaleY = scaleY;
    }

    /**
     * @param scaleX the scaleX to set
     */
    public void setScaleX(int scaleX) {
        this.scaleX = scaleX;
    }

    /**
     * @return the xAxisTitle
     */
    public String getxAxisTitle() {
        return xAxisTitle;
    }

    /**
     * @param xAxisTitle the xAxisTitle to set
     */
    public void setxAxisTitle(String xAxisTitle) {
        this.xAxisTitle = xAxisTitle;
    }

    /**
     * @return the yAxisTitle
     */
    public String getyAxisTitle() {
        return yAxisTitle;
    }

    /**
     * @param yAxisTitle the yAxisTitle to set
     */
    public void setyAxisTitle(String yAxisTitle) {
        this.yAxisTitle = yAxisTitle;
    }
}
